<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="ihis.model.*" %>
<%@ page import="ihis.core.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@page import="ihis.tms.client.IHISTMSClient"%>

<%@ include file="filter.jsp" %>
    
<%
	TaskFilter taskFilter = new TaskFilter(filter);
	taskFilter.setStartDate(null);
	ProjectScheduleRiskReport report = (ProjectScheduleRiskReport) core.getReport(IHISConstants.REPORT_RISK, taskFilter);
	List<Task> delayedRisks = report.getDelayedRiskList();
	List<Task> potentialRisks = report.getPotentialRiskList();
	
	// 샘플 데이터
	/*
	String[] ids = { "minho", "yoni230", "hee1460" };
	for(int i=0; i<10; i++){
		Task t = new Task();
		t.setDueDate(new Date());
		t.setOwnerId(ids[(int) (2*Math.random())]);
		t.setProjectId("" + ((int) (3*Math.random()) +1 ));
		t.setTeamId("" + ((int) (5*Math.random()) +1 ));
		t.setSubject("delayed task" + i);
		t.setTaskId("delayed task" +i);
		t.setType(IHISConstants.RISK_DELAYED);
		t.setProgressRatio((int) (100 * Math.random()));
		delayedRisks.add(t);
	}
	for(int i=0; i<10; i++){
		Task t = new Task();
		t.setDueDate(new Date());
		t.setOwnerId(ids[(int) (2*Math.random())]);
		t.setProjectId("" + ((int) (3*Math.random()) +1 ));
		t.setTeamId("" + ((int) (5*Math.random()) +1 ));
		t.setSubject("potential task" + i);
		t.setTaskId("potential task" +i);
		t.setProgressRatio((int) (100 * Math.random()));
		t.setType(IHISConstants.RISK_DELAYED);
		potentialRisks.add(t);
	}
	ownerType = "" + IHISConstants.OWNERTYPE_PROJECT;
	*/
	//  샘플 데이터 끝 
%>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Schedule Risk Report</title>
    <link rel="stylesheet" href="script/jquery.mobile-1.0a4.1.min.css"/>
    <link rel="stylesheet" href="script/ihis.css"/>
    <script src="script/jquery-1.6.min.js"></script>
    <script src="script/ihis.schedule.js"></script>
    <script src="script/jquery.mobile-1.0a4.1.min.js"></script>
</head>

<body>
	<div id="riskPage" data-role="page" data-theme="b">
		<div data-role="header" data-position="fixed">
			<a href="#" data-icon="back" data-role="button" id="backButton">Back</a>
			<h1><%= IHISConstants.OWNERTYPE_PERSON == Integer.parseInt(ownerType) ? userName + "님의 일정 위험 정보" : "조직 일정 위험 정보" %></h1>
			<a href="#" data-icon="home" data-role="button" id="homeButton">Home</a>
		</div>

		<div data-role="content"  data-theme="a">
			<ul data-role="listview" data-inset="false" data-filter="true">
<%
	if(delayedRisks != null){
%>
			<li data-role="list-divider">
				<h3>지연된 할일</h3>
				<span class="ui-li-count"><%= delayedRisks.size() %></span>
			</li>
<%
		for(Task t : delayedRisks){
			String tempUserName = tms.getUserName(t.getOwnerId());
			String tempTeamName = tms.getTeamName(t.getTeamId());
			String tempProjectName = tms.getProjectName(t.getProjectId());
			String dueDate = IHISUtil.dateToStr(t.getDueDate());
%>	
				<li>
					<h3><%= t.getSubject() %></h3>
					<p><%= tempTeamName + " / " + tempProjectName %></p>
					<p><%= t.getProgressRatio() + "% / " + dueDate %>
				</li>
<%
		}
	}
%>

<%
	if(potentialRisks != null){
%>
			<li data-role="list-divider">
				<h3>지연 가능성이 높은 할일</h3>
				<span class="ui-li-count"><%= potentialRisks.size() %></span>
			</li>
<%
		for(Task t : potentialRisks){
			String tempUserName = tms.getUserName(t.getOwnerId());
			String tempTeamName = tms.getTeamName(t.getTeamId());
			String tempProjectName = tms.getProjectName(t.getProjectId());
			String dueDate = IHISUtil.dateToStr(t.getDueDate());
%>	
				<li>
					<h3><%= t.getSubject() %></h3>
					<p><%= tempTeamName + " / " + tempProjectName %></p>
					<p><%= t.getProgressRatio() + "% / " + dueDate %>
				</li>
<%
		}
	}
%>
			</ul>
	
			
<%
	StringBuilder sb = new StringBuilder("Filter : ");
	if(!IHISUtil.isEmpty(startDate)){
		sb.append(startDate).append(" ");
	}
	if(!IHISUtil.isEmpty(endDate)){
		sb.append("~").append(endDate).append(" ");
	}
	if(!IHISUtil.isEmpty(user)){
		sb.append("/ ").append(userName).append("(").append(user).append(") " );
	}
	if(!IHISUtil.isEmpty(team)){
		sb.append("/ ").append(teamName).append(" ");
	}
	if(!IHISUtil.isEmpty(project)){
		sb.append("/ ").append(projectName).append(" ");
	}
	
%>
			<h5><%= sb.toString() %></h5>
		</div> 

	<script type="text/javascript">
    	$(function(){
    		$("#searchButton").bind("click", function(){
				<% 
					StringBuilder url = new StringBuilder("searchSchedule.jsp?filterFor=risk");
					url.append("&periodType=").append("" + filter.getPeriodType());
					url.append("&ownerType=").append("" + filter.getOwnerType());

					if(filter.getStartDate() != null){
						url.append("&").append("startDate=").append(new SimpleDateFormat("yyyy-MM-dd").format(filter.getStartDate()));
					}
					if(filter.getEndDate() != null){
						url.append("&").append("endDate=").append(new SimpleDateFormat("yyyy-MM-dd").format(filter.getEndDate()));
					}
					if(!IHISUtil.isEmpty(filter.getUserId())){
						url.append("&").append("user=").append(filter.getUserId());
					}
					if(!IHISUtil.isEmpty(filter.getProjectId())){
						url.append("&").append("project=").append(filter.getProjectId());
					}
					if(!IHISUtil.isEmpty(filter.getTeamId())){
						url.append("&").append("team=").append(filter.getTeamId());
					}
				%>
				var url = "<%= url %>";
				document.location = url;
    		});
    	});
    	
    	$(function(){
    		$("#riskChartButton").bind("click", function(){
				<% 
					url = new StringBuilder("riskChart.jsp?");
					url.append("periodType=").append("" + filter.getPeriodType());
					url.append("&ownerType=").append("" + filter.getOwnerType());

					if(filter.getStartDate() != null){
						url.append("&").append("startDate=").append(new SimpleDateFormat("yyyy-MM-dd").format(filter.getStartDate()));
					}
					if(filter.getEndDate() != null){
						url.append("&").append("endDate=").append(new SimpleDateFormat("yyyy-MM-dd").format(filter.getEndDate()));
					}
					if(!IHISUtil.isEmpty(filter.getUserId())){
						url.append("&").append("user=").append(filter.getUserId());
					}
					if(!IHISUtil.isEmpty(filter.getProjectId())){
						url.append("&").append("project=").append(filter.getProjectId());
					}
					if(!IHISUtil.isEmpty(filter.getTeamId())){
						url.append("&").append("team=").append(filter.getTeamId());
					}
				%>
				var url = "<%= url %>";
				document.location = url;
    		});
    	});
    </script>		
		
		<div data-role="footer" data-position="fixed">
			<a href="" id="searchButton" data-icon="check"  data-transition="slideup">Search</a>
			<a href="javascript:window.location.reload(true)" data-icon="refresh" data-rel="refresh" data-transition="flip">Refresh</a>
			<a href="" id="riskChartButton" data-icon="info"  data-transition="slideup">Chart</a>
		</div>
		
	</div>
	
	<!-- 검색 정보를 저장해둔다. : Task 목록을 위해 사용 -->
	<div>
		<form action="#" method="post" name="filterForm">
			<input type="hidden" name="startDate" value="<%= startDate%>"/>
			<input type="hidden" name="endDate" value="<%= endDate%>"/>
			<input type="hidden" name="periodType" value="<%= periodType%>"/>
			<input type="hidden" name="user" value="<%= user%>"/>
			<input type="hidden" name="team" value="<%= team%>"/>
			<input type="hidden" name="ownerType" value="<%= ownerType%>"/>
			<input type="hidden" name="taskType" value=""/>
		</form>
	</div>
</body>
</html>